import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter04/tools/keep_alive_wrapper.dart';

class PageViewKeepAlivePage extends StatefulWidget {
  const PageViewKeepAlivePage({super.key});

  @override
  State<PageViewKeepAlivePage> createState() => _PageViewKeepAlivePageState();
}

class _PageViewKeepAlivePageState extends State<PageViewKeepAlivePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("PageView KeepAlive 演示"),
      ),
      body: PageView.builder(
        scrollDirection: Axis.vertical,
        itemCount: 10,
        itemBuilder: (ctx, idx) {
          return KeepAliveWrapper(
            child: MyContainer(
              num: idx,
            ),
          );
        },
      ),
    );
  }
}

class MyContainer extends StatefulWidget {
  final int num;

  const MyContainer({super.key, this.num = 0});

  @override
  State<MyContainer> createState() => _MyContainerState();
}

class _MyContainerState extends State<MyContainer> {
  @override
  Widget build(BuildContext context) {
    if (kDebugMode) {
      print(widget.num);
    }
    return Center(
      child: Text(
        "第${widget.num + 1}屏",
        style: Theme.of(context).textTheme.headlineLarge,
      ),
    );
  }
}
